//
//  MVC-V.m
//  DesignPattern
//
//  Created by jun on 2021/4/16.
//

#import "MVC-V.h"

@implementation MVC_V

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
    }
    return self;
}

-(void)setModel:(MVC_M *)model{
    _model = model;
    
    [self updateView];
}

#pragma mark View 控制显示逻辑
-(void)updateView{
    if (self.model) {
        if (self.model.title) {
            self.backgroundColor = [UIColor greenColor];
        }else if(self.model.desc){
            self.backgroundColor = [UIColor blueColor];
        }else{
            self.backgroundColor = [UIColor redColor];
        }
    }else{
        self.backgroundColor = [UIColor whiteColor];
    }
}

#pragma mark View 刷新Model
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    
    if (self.model) {
        if (self.model.title) {
            self.model.title = nil;
            [self updateView];
        }else if(self.model.desc){
            self.model.desc = nil;
            [self updateView];
        }else{
            MVC_M *model = [MVC_M new];
            model.title = @"title";
            model.desc = @"desc";
            self.model = model;
        }
        
    }else{
        MVC_M *model = [MVC_M new];
        model.title = @"title";
        model.desc = @"desc";
        self.model = model;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
